package com.namibox.namiboxsdkdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.namibox.sdk.NamiboxManager;
import com.namibox.sdk.NamiboxManager.LoginCallback;
import com.namibox.sdk.NamiboxManager.LoginStatusCallback;
import com.namibox.sdk.NamiboxManager.PayCallback;
import com.namibox.sdk.NamiboxManager.ProductCallback;

public class MainActivity extends AppCompatActivity {
  TextView textView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.text1);
    findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        NamiboxManager.isNamiboxLogin(MainActivity.this, new LoginStatusCallback() {
          @Override
          public void onLoginStatus(boolean isLogin, String data) {
            textView.setText("获取到登录结果：\nisLogin=" + isLogin + "\ndata=" + data);
          }
        });
      }
    });
    findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        NamiboxManager.loginWithNamibox(MainActivity.this, new LoginCallback() {
          @Override
          public void onLoginResult(int resultCode, String resultMsg, String data) {
            textView.setText("获取到结果：\nresultCode=" + resultCode + "\nresultMsg=" + resultMsg + "\ndata=" + data);
          }
        });
      }
    });
    findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        NamiboxManager.queryProduct("109352", "1593", new ProductCallback() {
          @Override
          public void onQueryResult(final int resultCode, final String resultMsg, final String data) {
            runOnUiThread(new Runnable() {
              @Override
              public void run() {
                textView.setText("查询结果：\nresultCode=" + resultCode + "\nresultMsg=" + resultMsg + "\ndata=" + data);
              }
            });
          }
        });
      }
    });
    findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        NamiboxManager.startPayment(MainActivity.this, "1593", new PayCallback() {
          @Override
          public void onPayResult(boolean b, String s) {
            textView.setText("购买结果：\nresult=" + b + "\nproductid=" + s);
          }
        });
      }
    });
  }

  @Override
  protected void onPause() {
    super.onPause();
    NamiboxManager.onPause(this);
  }

  @Override
  protected void onResume() {
    super.onResume();
    NamiboxManager.onResume(this);
  }
}
